import java.util.Arrays;

public class J101701 {
    // key-value 模型
    public static class HashBucket {
        private static class Node {
            private int key;
            private int value;
            Node next;

            public Node(int key, int value) {
                this.key = key;
                this.value = value;
            }
        }


        private Node[]  array;
        private int size = 0;   // 当前的数据个数
        private static final double LOAD_FACTOR = 0.75;
        private static final int DEFAULT_SIZE = 8;//默认桶的大小

        public int put(int key, int value) {
            // write code here
            int index = key % array.length;
            Node cur = array[index];
            Node last = null;
            while (cur != null) {
                if (cur.key == key) {
                    int oldValue = cur.value;
                    cur.value = value;
                    return oldValue;
                }
                last = cur;
                cur = cur.next;
            }
            if (last == null) {
                array[index] = new Node(key, value);
            } else {
                last.next = new Node(key, value);
            }
            size++;
            if (loadFactor() > LOAD_FACTOR) {
                resize();
            }
            return -1;
        }


        private void resize() {
            Node[] newArray = new Node[array.length * 2];
            for (Node cur : array) {
                if (cur != null) {
                    int newindex = cur.key % newArray.length;
                    Node last = null;
                    Node tmp = newArray[newindex];
                    while (tmp != null) {
                        last = tmp;
                        tmp = tmp.next;
                    }
                    if (last == null) {
                        newArray[newindex] = cur;
                    } else {
                        last.next = cur;
                    }
                }
            }
            array = newArray;
        }


        private double loadFactor() {
            return size * 1.0 / array.length;
        }


        public HashBucket() {
            // write code here
            array = new Node[DEFAULT_SIZE];
        }


        public int get(int key) {
            // write code here
            for (Node cur : array) {
                while (cur != null && cur.key != key) {
                    cur = cur.next;
                }
                if (cur != null) {
                    return cur.value;
                }
            }
            return -1;
        }
    }

    public static void main(String[] args) {
        HashBucket hashmap = new HashBucket();
        hashmap.put(1,2);
        hashmap.put(0,1);
        hashmap.put(2,3);
        hashmap.put(3,4);
        hashmap.put(4,5);
        hashmap.put(5,6);
        hashmap.put(6,7);
        hashmap.put(7,8);
        hashmap.put(7,15);
        hashmap.put(20,20);
        System.out.println(Arrays.toString(hashmap.array));
        System.out.println(hashmap.get(999));
        System.out.println(hashmap.get(20));
    }
}
